package com.example.echoewish.ui.my_activity.my_comment_activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.echoewish.pojo.MyComment;
import com.example.echoewish.R;

import java.util.List;

public class MyCommentAdapter extends ArrayAdapter<MyComment> {
    Context activity;
    int resource;

    private  TextView myCommentTitleTextView;
    private TextView myCommentContentTextView;
    private TextView myCommentTimeTextView;
    private Button deleteCommentButton;
    private ButtonClickListener buttonClickListener;

    public MyCommentAdapter(@NonNull Context context, int resource, @NonNull List<MyComment> objects) {
        super(context, resource, objects);
        this.activity=context;
        this.resource=resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view= LayoutInflater.from(activity).inflate(resource,parent,false);
        myCommentTitleTextView=view.findViewById(R.id.my_comment_title_text_view);
        myCommentContentTextView=view.findViewById(R.id.my_comment_content_text_view);
        myCommentTimeTextView=view.findViewById(R.id.my_comment_time_text_view);
        deleteCommentButton=view.findViewById(R.id.my_comment_delete_button);

        MyComment myComment=getItem(position);
        if(myComment!=null)
        {
            myCommentTitleTextView.setText(myComment.postTitle);
            myCommentContentTextView.setText(myComment.content);
            myCommentTimeTextView.setText(myComment.time);
        }
        deleteCommentButton.setTag(myComment);

        deleteCommentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonClickListener.buttonClickListener(position);
            }
        });
        return view;
    }
    public void setButtonClickListener(ButtonClickListener buttonClickListener){
        this.buttonClickListener=buttonClickListener;
    }

    public interface ButtonClickListener
    {
        void buttonClickListener(int nowPosition);
    }

}
